package com.yunshipei.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yunshipei.enterplorer.R;
import com.yunshipei.ui.view.SetTextSizeView;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TextSizeSettingFragment extends Fragment implements SetTextSizeView.OnPointResultListener {

    private static final String ARGS_DEFAULT_POSITION = "com.yunshipei.enterplorer.default.text.zoom.position";

    @Bind(R.id.tv_normal_zise)
    protected TextView tvNormalSizeView;
    @Bind(R.id.ssv_view)
    protected SetTextSizeView mSetTextSizeView;

    private int defaultPosition = 0;
    private OnFragmentInteractionListener mFragmentInteractionListener;

    public static TextSizeSettingFragment newInstance(int defaultPosition) {
        TextSizeSettingFragment fragment = new TextSizeSettingFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARGS_DEFAULT_POSITION, defaultPosition);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mFragmentInteractionListener = (OnFragmentInteractionListener) context;
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mFragmentInteractionListener == null) {
            Fragment fragment = getParentFragment();
            if (fragment != null && fragment instanceof OnFragmentInteractionListener) {
                mFragmentInteractionListener = (OnFragmentInteractionListener) fragment;
            }
        }
        defaultPosition = getArguments().getInt(ARGS_DEFAULT_POSITION, 1);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_text_size_setting, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvNormalSizeView.post(new Runnable() {
            @Override
            public void run() {
                int len = mSetTextSizeView.getItemWidth() + (!isAdded() ? 30 : getActivity().getResources().getDimensionPixelOffset(R.dimen.font_size_radius));
                int tvWidth = tvNormalSizeView.getWidth();
                int margin = len - tvWidth / 2;
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tvNormalSizeView.getLayoutParams();
                params.setMarginStart(margin);
                tvNormalSizeView.setLayoutParams(params);
            }
        });
        mSetTextSizeView.setOnPointResultListener(this);
        mSetTextSizeView.setCurrentProgress(defaultPosition);
    }

    @Override
    public void onPointResult(int position) {
        if (mFragmentInteractionListener != null) {
            mFragmentInteractionListener.changeWebSize(position);
        }
    }

    public void setDefaultFontZoomPosition(int position) {
        this.defaultPosition = position;
        if (mSetTextSizeView != null) {
            mSetTextSizeView.setCurrentProgress(position);
        }
    }

    public interface OnFragmentInteractionListener {
        void changeWebSize(int position);
    }
}
